<?php 
namespace app\index\model;
use think\Model;
use think\Db;

class User extends Model
{
	


	public function chat($data)
	{
		return Db::name('chat')->insertGetId($data);
	}



	/**
	 * 
	 * 
	 * @param [type] $data
	 * @return void
	 */
	public function envelope($type, $data)
	{
		Db::startTrans();
		try{
			
            $wid = Db::name('income')->insertGetId($data);
			//更新余额
			Db::name('company')->where('id', $data['company_id'])->setDec('account', $data['income']);  //公司账户减少

			Db::name('user')->where('id', $data['share_id'])->setInc('wallet', $data['income']);  //个人账户增加

			Db::name('share')->where(['type'=>$type, 'share_id'=>$data['share_id'], 'user_id'=>$data['user_id'], 'position_id'=>$data['position_id']])->setInc('status');
        	// 提交事务
		    Db::commit();
			return $wid;
			
		} catch (\Exception $e) {
		    // 回滚事务
		    Db::rollback();
		    return false;
		}
		
	}


	/**
	 * 提现申请
	 */
	public function withdraw($userId, $type, $open_bank, $bank_name, $name, $bank_card, $amount)
 	{
 		Db::startTrans();
		try{
			
			
			
			//查询用户是否有绑定的转账账户
			$where['user_id'] = $userId;
            $where['type'] = $type;
            $info = Db::name('account')->where($where)->find();
			//更新余额
			$wid = Db::name('user')->where('id', $userId)->setDec('income', $amount);

        	// 提交事务
		    Db::commit();
			return $wid;
			
		} catch (\Exception $e) {
		    // 回滚事务
		    Db::rollback();
		    return false;
		}
	}
	




	public function appinstallcode($userId, $id, $imei)
 	{
 		Db::startTrans();
		try{
			$integral = 10;
			//返回用户积分
			Db::name('user')->where('id', $userId)->setInc('integral', $integral);
			//添加积分记录
			$intdata['user_id'] = $userId;
			$intdata['type']	= 1;
			$intdata['title']	= '推荐好友赠送积分';
			$intdata['num']  	= $integral;
			$intdata['ctime'] 	= time();
			$wid = Db::name('integral')->insert($intdata);
			
			
			//更新
			Db::name('spread')->where('id', $id)->update(['imei'=>$imei]);

        	// 提交事务
		    Db::commit();
			return $wid;
			
		} catch (\Exception $e) {
		    // 回滚事务
		    Db::rollback();
		    return false;
		}
	}


}